import http from '@/api/config'
export default {
  state: {
    echartData: {
      order: {
        xData: [],
        series: []
      },
      user: {
        xData: [],
        series: []
      },
      video: {
        series: []
      },
    }
  },
  mutations: {
    homeData(state, Data) {
      state.echartData.order.xData = Data.orderData.date
      state.echartData.order.series = Data.orderData.newKeyArray
      state.echartData.user.xData = Data.userData.xData
      state.echartData.user.series = Data.userData.series
      state.echartData.video.series = Data.video
    }
  },
  actions: {
    getHomeData(context) {
      http.get('/home/getData').then(
        res => {
          let Data = {}
          let ress = res.data.data
          Data.orderData = ress.orderData
          // context.state.echartData.order.xData = orderData.date   //可以直接在actions修改state的数据   但是vuex不建议  
          let keyArray = Object.keys(Data.orderData.data[0])
          Data.orderData.newKeyArray = []
          keyArray.forEach(key => {
            Data.orderData.newKeyArray.push({
              name: key === 'wechat' ? '小程序' : key,
              data: Data.orderData.data.map(item => item[key]),
              type: 'line'
            })
          })


          Data.userData = ress.userData
          Data.userData.xData = Data.userData.map(item => item.date)
          Data.userData.series = []
          Data.userData.series.push({
            name: '新增用户',
            data: Data.userData.map(item => item.new),
            type: 'bar'
          })
          Data.userData.series.push({
            name: '活跃用户',
            data: Data.userData.map(item => item.active),
            type: 'bar',
            barGap: 0
          })


          Data.video = []
          Data.video.push({
            data: ress.videoData,
            type: 'pie'
          })
          context.commit('homeData', Data)

        }
      )
    }
  },
  getters: {

  }
}